//
//  ApplicationCashViewController.m
//  BaishidaiKnight
//
//  Created by mac book on 2019/1/28.
//  Copyright © 2019年 sy. All rights reserved.
//

#import "ApplicationCashViewController.h"

@interface ApplicationCashViewController ()<UITextFieldDelegate>

@property(nonatomic,strong) UILabel* titleLabel;
@property(nonatomic,strong) UITextField* cardTF;
@property(nonatomic,strong) UITextField* moneyTF;


@property(nonatomic,strong) UIButton* confirmBtn;
@property(nonatomic,strong) TTTAttributedLabel* markLabel;


@end

@implementation ApplicationCashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = @"申请提现";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self creatSubViews];
    
    
}


#pragma mark - 
-(void)creatSubViews{
    
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.cardTF];
    [self.view addSubview:self.moneyTF];
    
    [self.view addSubview:self.confirmBtn];
    [self.view addSubview:self.markLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.height.mas_equalTo(50);
    }];
    
    
    [self.cardTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(self.titleLabel);
    }];
    
    [self.moneyTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cardTF.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(self.cardTF);
    }];
    
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.moneyTF.mas_bottom).offset(50);
        make.left.right.equalTo(self.titleLabel);
        make.height.equalTo(self.titleLabel);
    }];
    
    [self.markLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.confirmBtn.mas_bottom).offset(10);
        make.left.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.view);
    }];
    
    
    
    
}


#pragma mark - <UITextFieldDelegate>
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if(range.length == 0 && range.location == 0 && [string isEqualToString:@"0"] ){
        return NO;
    }else if([CommonUntils isEmptyOrNULLOrnil:string]){
        return YES;
    }else if([self.userInfoModel.account integerValue]<[textField.text integerValue]){
        return NO;
    }else if ([string isEqualToString:@"."]){
        return NO;
    }
    return YES;
}

#pragma mark - 申请提现
-(void)confirmBtnTouch:(UIButton *)sender{
    if([self checkForm]){
        [self showHUD];
        
        NSDictionary* arg = @{
                              @"ince":@"set_emp_deposit",
                              @"emp_id":[UserHandle shareInstance].userId,
                              @"amount":self.moneyTF.text
                              };
        
        [BSDNetTool PostURL:kHost  parameters:arg succeed:^(NSDictionary *data) {
            NSLog(@"data = %@",data);
            NSInteger flag = [[data objectForKey:@"status"]integerValue];
            if(flag == 1){

                [self hidHUD:[data objectForKey:@"sucess"]];
                self.moneyTF.text = @"";
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [self hidHUD:[data objectForKey:@"fail"]];
            }
            
        } failure:^(NSError *error) {
            [self hidHUD:@"网络异常"];
        }];

    }
}

#pragma mark -
-(BOOL)checkForm{
    if([CommonUntils isEmptyOrNULLOrnil:self.moneyTF.text]){
        [self alertHUD:@"请输入提现金额"];
        return NO;
    }else if([self.moneyTF.text integerValue]<50){
        [self alertHUD:@"至少提现50元"];
        return NO;
    }
    return YES;
}

#pragma mark - 懒加载
-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont systemFontOfSize:12.f];
        _titleLabel.textColor = kMyColor(64, 64, 64);
        _titleLabel.text = self.userInfoModel.top_note;

        
    }
    return _titleLabel;
}

-(UITextField *)cardTF{
    if(!_cardTF){
        _cardTF = [[UITextField alloc]init];
        _cardTF.backgroundColor = [UIColor whiteColor];
        _cardTF.borderStyle=UITextBorderStyleNone;
        _cardTF.layer.borderColor =kMyColor(231, 231, 231).CGColor;
        _cardTF.layer.borderWidth = 1.0f;
        _cardTF.layer.masksToBounds = YES;
        _cardTF.userInteractionEnabled = NO;
        _cardTF.font = [UIFont systemFontOfSize:14.f];
        _cardTF.textColor = kMyColor(64, 64, 64);
        
        if (self.userInfoModel.bank_name.length != 0) {
            NSMutableString *str = [[NSMutableString  alloc] initWithString:[NSString stringWithFormat:@"%@ %@",self.userInfoModel.bank_name,self.userInfoModel.bank]];
            _cardTF.text = str;
        }
       
        UILabel* label = [[UILabel alloc]init];
        label.frame = CGRectMake(0, 0, 15, 50);
        label.font = [UIFont systemFontOfSize:14.f];
        label.textColor = kMyColor(64, 64, 64);
        label.textAlignment = NSTextAlignmentCenter;
        _cardTF.leftView = label;
        _cardTF.leftViewMode =UITextFieldViewModeAlways;
    }
    return _cardTF;
}

-(UITextField *)moneyTF{
    if(!_moneyTF){
        _moneyTF = [[UITextField alloc]init];
        _moneyTF.backgroundColor = [UIColor whiteColor];
        _moneyTF.borderStyle=UITextBorderStyleNone;
        _moneyTF.delegate = self;
        _moneyTF.font = [UIFont systemFontOfSize:14.f];
        _moneyTF.keyboardType = UIKeyboardTypeNumberPad;
        _moneyTF.placeholder =@"请输入提现金额";
        _moneyTF.textColor = kMyColor(64, 64, 64);
        UIView* leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 50.f)];
        UILabel* label = [[UILabel alloc]init];
        label.frame = CGRectMake(15, 0, 70, 50);
        label.font = [UIFont systemFontOfSize:14.f];
        label.textColor = kMyColor(64, 64, 64);
        label.text = @"提现金额";
        label.textAlignment = NSTextAlignmentLeft;
        [leftView addSubview:label];
        label = [[UILabel alloc]initWithFrame:CGRectMake(90-8, 15, 2, 20.f)];
        label.backgroundColor = kMyColor(248, 159, 39);
        [leftView addSubview:label];
        _moneyTF.leftView = leftView;
        _moneyTF.leftViewMode =UITextFieldViewModeAlways;
    }
    return _moneyTF;
}



-(UIButton *)confirmBtn{
    if(!_confirmBtn){
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmBtn.backgroundColor = kMyColor(248, 159, 39);
        [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_confirmBtn setTitle:@"确认提现" forState:UIControlStateNormal];
        [_confirmBtn addTarget:self action:@selector(confirmBtnTouch:) forControlEvents:UIControlEventTouchUpInside];
        _confirmBtn.layer.masksToBounds = YES;
        _confirmBtn.layer.cornerRadius = 5.f;
    }
    return _confirmBtn;
}

-(TTTAttributedLabel *)markLabel{
    if(!_markLabel){
        _markLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectZero];
        _markLabel.font = [UIFont systemFontOfSize:12.f];
        _markLabel.textColor = kMyColor(64, 64, 64);
        _markLabel.numberOfLines = 0;
        _markLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _markLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop;
        _markLabel.lineSpacing = 3;
        _markLabel.text = self.userInfoModel.bottom_note;
        
    }
    return _markLabel;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
